import 'package:flutter/material.dart';

class NetWorkImage extends StatefulWidget {
  final String uri;
  final double? width;
  final double? height;
  final String defualtImageUri;

  const NetWorkImage(this.uri,{Key? key,this.width, this.height, required this.defualtImageUri}) : super(key: key);
  

  @override
  _NetWorkImageState createState() => _NetWorkImageState();
}

class _NetWorkImageState extends State<NetWorkImage> {
  late Image _image;

  @override
  void initState() {
    super.initState();
    _image = Image.network(widget.uri,width: widget.width,height: widget.height,);
    var resolve = _image.image.resolve(ImageConfiguration.empty);
    resolve.addListener(ImageStreamListener((_,__){
      // 成功
    },onError: (_,__){
      // 失败
      setState(() {
        _image = Image.asset(
          widget.defualtImageUri,
          width: widget.width,
          height: widget.height,
        );
      });
    }));
  }

  @override
  Widget build(BuildContext context) {
    return _image;
  }
}